﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
namespace OracleDAL.Sec
{
    public class ManageLog
    {
        public int LogonLog(string logisn, string usrisn, string username, string userip, string usermac, string logontime,string logouttime, string logonsucceed)
        {

            string sql = "insert into sec_logonlog (LOGISN,USERISN,USERNAME,USERIP,USERMAC,LOGONTIME,LOGONSUCCEED) ";
            if (logouttime == "")
            {
                sql += " values('" + logisn + "','" + usrisn + "','" + username + "','" + userip + "','" + usermac + "', to_date('" + logontime + "','yyyy-mm-dd  hh24:mi:ss'),'" + logonsucceed + "')";
            }
            else
            {
                sql += " values('" + logisn + "','" + usrisn + "','" + username + "','" + userip + "','" + usermac + "', to_date('" + logontime + "','yyyy-mm-dd  hh24:mi:ss'),to_date('" + logouttime + "','yyyy-mm-dd  hh24:mi:ss'),'" + logonsucceed + "')";
            }
            int result = DBUtility.OracleHelper.ExecuteSql(sql);
            return result;
           

        }
        public int OperateLog(string logisn, string usrisn, string username,string operationtime,string ipaddr,string macaddr,string operatename,string operateresource,string operatecontent,string operatersult)
        {

            string sql = "insert into sec_operatelog ( LOGISN,USERISN,USERNAME,OPERATIONTIME,IPADDR,MACADDR,OPERATENAME,OPERATERESOURCE,OPERATECONTENT,OPERATERESULT) ";
            sql += " values('" + logisn + "','" + usrisn + "','" + username + "',to_date('"+operationtime+"','yyyy-mm-dd  hh24:mi:ss'),'" + ipaddr + "','" + macaddr + "','" + operatename + "','" + operateresource + "','" + operatecontent + "','" + operatersult + "')";
            int result = DBUtility.OracleHelper.ExecuteSql(sql);
            return result;
        }
        public int modifyLogonLog(string logisn,string logoutTime)
        {
            string sql = "update sec_logonlog set LOGOUTTIME=to_date('"+logoutTime+"','yyyy-mm-dd  hh24:mi:ss') where LOGISN='" + logisn + "'";
            int result = DBUtility.OracleHelper.ExecuteSql(sql);
            return result;  
        }    
        public int runLog(string id,string username,string ip,string loginfo,string logtype,DateTime  logtime ,string logLevel)
        {
            string sql = "insert  into runlog (id,username,ip,loginfo,logtype,logtime,logLevel) values('" + id + "','" + username + "','" + ip + "','" + loginfo + "','" + logtype + "',to_date('" + logtime + "','yyyy-mm-dd  hh24:mi:ss'),'" + logLevel + "')";

            int result = DBUtility.OracleHelper.ExecuteSql(sql);
            return result;
        }
        public DataSet getLog(string logType, string strStart, string strEnd)
        {
            DataSet ds = new DataSet();
            string strSql = "";
            if (logType == "operate")
            {
                strSql = "select USERNAME as 操作人,OPERATIONTIME as 操作时间,IPADDR as IP地址,MACADDR as MAC地址,OPERATENAME as 操作内容,OPERATERESOURCE as 操作项目,OPERATERESULT 是否成功";
                strSql += " from sec_operatelog";
                strSql += " where OPERATIONTIME between " + strStart + " and " + strEnd + "  order by  OPERATIONTIME desc ";

                ds = DBUtility.OracleHelper.Query(strSql);
            }
            else if (logType == "logon")
            {
                strSql = "select USERNAME as 登录人,USERIP as IP地址,USERMAC as MAC地址,LOGONTIME as 登录时间,LOGOUTTIME as 退出时间,LOGONSUCCEED as 是否成功 ";
                strSql += " from sec_logonlog";
                strSql += " where LOGONTIME between " + strStart + " and " + strEnd + " order by  LOGONTIME desc ";

                ds = DBUtility.OracleHelper.Query(strSql);
            }
            else if (logType == "run")
            {
                strSql = "select username as 操作人,logtime as 操作时间,ip as IP地址,logtype as 类型,loglevel as 级别,loginfo as 操作内容";
                strSql += " from runlog";
                strSql += " where logtime between " + strStart + " and " + strEnd + "  order by  logtime desc ";

                ds = DBUtility.OracleHelper.Query(strSql);
            }
            return ds;
        }
    }
}
